function finalRes = outSamplePerformance(X,Y,params,Ntrain,type)

fprintf('\n=========== OUT SAMPLE PERFORMANCE ===========\n');

global Standardize;
COpt = params(1);
gammaOpt = params(2);
if(size(params,2)>1)
    params = params(1,:);
end
fprintf('With paramerter C = %f, gamma = %f \n\n',COpt,gammaOpt);
N = size(X,1); Ntest= N - Ntrain;

Xtrain = X(1:Ntrain,:);
Ytrain = Y(1:Ntrain);
res = zeros(Ntest,3);


Xtrain2 = Xtrain;
Ytrain2 = Ytrain;
for i = 1: Ntest   
    SVMModel = fitcsvm(Xtrain2,Ytrain2,'KernelFunction','rbf','BoxConstraint'...
        ,COpt,'KernelScale',gammaOpt,'Standardize',Standardize);
    % 1. get predict result
    Xtest2 = X(Ntrain+i,1:end); %no window        
    [label ,scores] = predict(SVMModel,Xtest2);
    res(i,:) = [label Y(Ntrain+i) scores(1)];
    
    % 2. update     
%     no window
    Xtrain2 = X(1:Ntrain+i,1:end);
    Ytrain2 = Y(1:Ntrain+i,:);    
    %window    
%     Xtrain2 = X(i:Ntrain+i,1:end);
%     Ytrain2 = Y(i:Ntrain+i,:);    
end

p11 = sum(res(:,1).*res(:,2))./ sum(res(:,2));
p00 = sum((~res(:,1)).*(~res(:,2)))./ sum(~res(:,2));

fprintf('pTrue = %2.4f, p1100 = %2.4f,\np(1|1) = %2.4f,p(0|0) = %2.4f\n',...
    sum(res(:,1)==res(:,2))./Ntest, (p11+p00)/2, p11, p00);

if(strcmp(type,'rbf'))
    SVMModel_2 = fitcsvm(Xtrain,Ytrain,'KernelFunction','rbf','BoxConstraint'...
        ,COpt,'KernelScale',gammaOpt,'Standardize',Standardize);
else    
    SVMModel_2 = fitcsvm(Xtrain,Ytrain,'KernelFunction','mysigmoid','BoxConstraint'...
        ,COpt,'Standardize',Standardize);
end
label1 = predict(SVMModel_2,X(Ntrain+1:end,:));    
res1 = [label1 Y(Ntrain+1:end)];


p11_2 = sum(res1(:,1).*res1(:,2))./ sum(res1(:,2));
p00_2 = sum((~res1(:,1)).*(~res1(:,2)))./ sum(~res1(:,2));

% [sum(res(:,1)==res(:,2))./Ntest p11 p00 (p11+p00)/2 sum(~res(:,2))./Ntest]
fprintf('==================\npTrue = %2.4f, p1100 = %2.4f,\np(1|1) = %2.4f,p(0|0) = %2.4f\n',...
    sum(res1(:,1)==res1(:,2))./Ntest, (p11_2+p00_2)/2, p11_2, p00_2);

finalRes = [sum(res(:,1)==res(:,2))./Ntest, (p11+p00)/2, p11, p00;
    sum(res1(:,1)==res1(:,2))./Ntest, (p11_2+p00_2)/2, p11_2, p00_2];

end